﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinimalistFoodStreet.BackEnd.Infrastructure.Tools
{
    public static class Tools
    {
        /// <summary>
        /// 用指定分隔符连接字符串数组
        /// </summary>
        /// <param name="strings"></param>
        /// <param name="concat"></param>
        /// <returns></returns>
        public static string Join(List<string> strings, string concat=",")
        {
            if (strings.Count == 0)
            {
                return "";
            }
            string result = "";
            foreach (string s in strings)
            {
                result += s + concat;
            }
            return result.Substring(0, result.Length - concat.Length);
        }
        /// <summary>
        /// 生成无序的GUID
        /// </summary>
        /// <returns></returns>
        public static string Guid_1()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }
        /// <summary>
        /// 生成有序的GUID
        /// </summary>
        /// <returns></returns>
        public static string Guid_2()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Guid.NewGuid().ToString().Replace("-", "").Substring(10);
        }
        /// <summary>
        /// 将时间转化成固定格式字符串
        /// </summary>
        /// <param name="time">时间对象</param>
        /// <param name="split1">日期分隔符</param>
        /// <param name="split2">时间分隔符</param>
        /// <returns></returns>
        public static string TimeToString(DateTime? time, string split1 = "-", string split2 = ":")
        {
            if (time == null)
            {
                return "";
            }
            DateTime t = (DateTime)time;
            return GT(t.Year, 4) + split1 + GT(t.Month) + split1 + GT(t.Day) + " " + GT(t.Hour) + split2 + GT(t.Minute) + split2 + GT(t.Second)
                ;
        }
        //获取时间部分的正确显示 
        private static string GT(int partTime, int length = 2)
        {
            string result = "";
            for (int i = 0; i < length - partTime.ToString().Length; i++)
            {
                result += "0";
            }
            result += partTime.ToString();
            return result;
        }
    }
}
